package com.ruoyi.oar.controller.app;

import java.util.List;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.RepeatSubmit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.oar.domain.dto.OarRoomsDTO;
import com.ruoyi.oar.domain.query.OarRoomsQuery;
import com.ruoyi.oar.service.OarRoomsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.*;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.Optional;

/**
 * 房间Controller
 * 
 * @author ruoyi
 * @date 2022-02-17
 */
@Api(tags = "APP房间")
@RestController
@RequestMapping("/app/oar-rooms")
public class AppRoomsController extends BaseController
{
    @Autowired
    private OarRoomsService oarRoomsService;

    /**
     * 查询房间列表
     */
    @ApiOperation("查询房间列表")
    @GetMapping
    public TableDataInfo<OarRoomsDTO> list(OarRoomsQuery oarRoomsQuery){
        logger.debug("查询房间列表,查询条件oarRoomsQuery={}",JSONObject.toJSONString(oarRoomsQuery));
        startPage();
        return oarRoomsService.findAll(oarRoomsQuery);
    }

    /**
     * 导出房间列表
     */
    @ApiOperation("导出房间列表")
    @Log(title = "房间", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OarRoomsQuery oarRoomsQuery){
        logger.debug("查询房间列表,导出条件oarRoomsQuery={}",JSONObject.toJSONString(oarRoomsQuery));
        List<OarRoomsDTO> list = oarRoomsService.selectOarRoomsList(oarRoomsQuery);
        ExcelUtil<OarRoomsDTO> util = new ExcelUtil<OarRoomsDTO>(OarRoomsDTO.class);
        return util.exportExcel(list, "房间数据");
    }

    /**
     * 获取房间详细信息
     */
    @ApiOperation("获取房间详细信息")
    @GetMapping(value = "/{id}")
    public Response<OarRoomsDTO> getInfo(@PathVariable("id") @ApiParam("房间ID") String id) {
        logger.debug("获取房间详细信息,id={}",id);
        checkId(id);
        return Response.success(oarRoomsService.selectOarRoomsById(id).orElseThrow(()->new ServiceException("无效的ID")));
    }

    /**
     * 新增房间
     */
    @ApiOperation("新增房间")
    @Log(title = "房间", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody OarRoomsDTO oarRoomsDTO) {
        logger.debug("新增房间,oarRoomsDTO={}",JSONObject.toJSONString(oarRoomsDTO));
        return toAjax(oarRoomsService.insertOarRooms(oarRoomsDTO));
    }

    /**
     * 修改房间
     */
    @ApiOperation("修改房间")
    @PreAuthorize("@ss.hasPermi('oar:rooms:edit')")
    @Log(title = "房间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OarRoomsDTO oarRoomsDTO) {
        logger.debug("修改房间,oarRoomsDTO={}",JSONObject.toJSONString(oarRoomsDTO));
        checkId(oarRoomsDTO.getId());
        return toAjax(oarRoomsService.updateOarRooms(oarRoomsDTO));
    }

    /**
     * 删除房间
     */
    @ApiOperation("删除房间")
    @Log(title = "房间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        logger.debug("删除房间,ids={}",JSONObject.toJSONString(ids));
        Arrays.asList(ids).forEach(this::checkId);
        return toAjax(oarRoomsService.deleteOarRoomsByIds(ids));
    }

    /**
     * 校验
     * @param id
     */
    private void checkId(String id) {
        Optional.of(id).orElseThrow(() -> new ServiceException("ID不能为空"));
        oarRoomsService.selectOarRoomsById(id).orElseThrow(() -> new ServiceException("无效的ID"));
    }
}
